package controller;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.JTable;

import model.FileTableModel;
import model.SearchTableModel;
import view.MainFrame;

public class SearchListener implements KeyListener {
	
	private MainFrame frame;
	public SearchListener(MainFrame frame) {
		
		this.frame = frame;
	}
	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode() == 10){	
			int rowNumber = ((JTable)arg0.getSource()).getSelectedRow();
			File newFile = ((SearchTableModel)((JTable)arg0.getSource()).getModel()).getSelectedRow(rowNumber);
			if (!newFile.isDirectory()) {
				newFile = newFile.getParentFile();
			}				
			((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).Clear();
			((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).Create(newFile);	
			((FileTableModel) frame.getMainPanel().getPrimary().getDiskContent().getTable().getModel()).fireTableDataChanged();
			
			
			frame.getMainPanel().getPrimary().getDiskContent().setActualFile(newFile);
			frame.getMainPanel().getPrimary().getRelativePath().setText(newFile.getAbsolutePath());
			frame.getMainPanel().getPrimary().setStatusBar(newFile);
		}
		
	}
	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	

}
